package com.yunnuo.system.controller;

import com.yunnuo.common.controller.BaseController;
import com.yunnuo.common.web.ResultData;
import com.yunnuo.common.web.page.TableDataInfo;
import com.yunnuo.system.domain.DictData;
import com.yunnuo.system.service.IDictDataService;
import net.sf.json.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @api {title} 数据字典数据comtroller
 * @apiName DictDataController
 * @apiGroup System-DictDataController
 * @apiDescription Created by 魏洪福(15064000912@163.com) on  2018/5/25.
 */
@Controller
@RequestMapping("/system/dict/data")
public class DictDataController extends BaseController
{
    private String prefix = "system/dict/data";

    @Autowired
    private IDictDataService dictDataService;

    @RequiresPermissions("system:dict:view")
    @GetMapping()
    public String dictData()
    {
        return prefix + "/data";
    }

    @GetMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public TableDataInfo list(DictData dictData)
    {
        startPage();
        List<DictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @GetMapping("/edit/{dictCode}")
    public String edit(@PathVariable("dictCode") Long dictCode, Model model)
    {
        DictData dict = dictDataService.selectDictDataById(dictCode);
        model.addAttribute("dict", dict);
        return prefix + "/edit";
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @GetMapping("/add/{dictType}")
    public String add(@PathVariable("dictType") String dictType, Model model)
    {
        model.addAttribute("dictType", dictType);
        return prefix + "/add";
    }

    /**
     * 保存字典类型
     */
    @RequiresPermissions("system:dict:save")
    @PostMapping("/save")
    @ResponseBody
    public JSON save(DictData dict)
    {
        ResultData data = new ResultData();
        int rows =dictDataService.saveDictData(dict);
        if (rows > 0)
        {
            data.setStat(ResultData.STAT_FAILURE);
            data.setData(rows);
            data.setMessage("操作成功！");
        }else{
            data.setStat(ResultData.STAT_SUCCESS);
            data.setData("");
            data.setMessage("操作成功！");
        }
        return data.toJson();
    }
    
    /**
     * 删除
     */
    @RequiresPermissions("system:dict:remove")
    @RequestMapping("/remove/{dictCode}")
    @ResponseBody
    public JSON remove(@PathVariable("dictCode") Long dictCode)
    {
        ResultData data = new ResultData();
        DictData dictData = dictDataService.selectDictDataById(dictCode);
        if (dictData == null)
        {
            data.setStat(ResultData.STAT_FAILURE);
            data.setData("");
            data.setMessage("数据字典不存在！");
        }
        if (dictDataService.deleteDictDataById(dictCode) > 0)
        {
            data.setStat(ResultData.STAT_SUCCESS);
            data.setData("");
            data.setMessage("操作成功！");
        }
        return data.toJson();
    }

    @RequiresPermissions("system:dict:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public JSON batchRemove(@RequestParam("ids[]") Long[] ids)
    {
        ResultData data = new ResultData();
        int rows = dictDataService.batchDeleteDictData(ids);
        if (rows > 0)
        {
            data.setStat(ResultData.STAT_SUCCESS);
            data.setData(rows);
            data.setMessage("操作成功！");
        }else{
            data.setStat(ResultData.STAT_FAILURE);
            data.setData("");
            data.setMessage("操作失败！");
        }
        return data.toJson();
    }
}
